package org.gxuwz.core.dao.impl;

import java.util.List;

import org.gxuwz.core.dao.IReimbursementDao;
import org.gxuwz.core.entity.TCommissure;
import org.gxuwz.core.entity.TReimbursement;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("ReimbursementDao")
public class ReimbursementDao  extends BaseDao implements IReimbursementDao{
	@Autowired
	private SessionFactory sessionFactory;
	public Session getSession() {
		Session session = sessionFactory.openSession();
		return session;
	}
	
	@Override          //添加报销记录
	public void addReimbursemen(TReimbursement reim) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(reim);
		tx.commit();
		session.close();
		
	}

	@Override           // 显示所有报销记录,若有输入条件，则根据报销单号和身份证号查询
	public List<TReimbursement> getAll(String name, String perscode) {
		List<TReimbursement> list = null;
		Session session = getSession();
		String hql = "select * from t_reimbursement where 1=1";
		if(perscode!=null&&perscode!=""){
			hql += " and perscode like '%"+perscode+"%'";
		}
		if(name!=null&&name!=""){
			hql += " and name like '%"+name+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TReimbursement.class);
		//Query query = session.createQuery(hql);
		list = query.list();
		session.close();
		for(int i=0;i<=list.size();i++){
			System.out.println(list.get(i)+"wwwwwwwwwwwwwwwwwwwwwwww");
		}
		return list;
	}
	
	@Override           // 显示所有报销记录
	public List<TReimbursement> getAllJiLu() {
		List<TReimbursement> list = null;
		Session session = getSession();
		String hql = "select * from t_reimbursement where 1=1";
		Query query = session.createSQLQuery(hql).addEntity(TReimbursement.class);
		//Query query = session.createQuery(hql);
		list = query.list();
		session.close();
		return list;
	}

	@Override			 // 查找单条报销记录
	public TReimbursement getByPerscode(String perscode) {
		TReimbursement reim = null;
		List<TReimbursement> list = null;
		Session session = getSession();
		String hql = "select * from t_commissure where 1=1";
		//String hql = "from TFamily  where 1=1";
		if(perscode!=null&&perscode!=""){
			hql += " and perscode like '%"+perscode+"%'";
		}
		Query query = session.createSQLQuery(hql).addEntity(TReimbursement.class);
		list = query.list();
		session.close();
		if(list.size()>=0){
			reim =list.get(0);
		}
	    return reim;
	}

	@Override				 // 修改档案信息
	public void update(TReimbursement reim) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.update(reim);
		tx.commit();
		session.close();
		
	}

	@Override				 // 删除档案信息
	public void delete(TReimbursement reim) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(reim);
		tx.commit();
		session.close();
		
	}

}
